package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.OtherPurchaseRecord;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/4/8.
 *
 */
@Repository
public class OtherPurchaseRecordDao extends MyBatisDao<OtherPurchaseRecord>{

    /**
     * 养殖端的分页查询
     * @param offset 页码
     * @param limit 每页多少条
     * @param criteria 参数
     * @return Paging<OtherPurchaseRecord>
     */
    public Paging<OtherPurchaseRecord> pagingOtherPurchaseRecordsFarm(Integer offset, Integer limit, Map<String, Object> criteria){
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(sqlId("countFarm"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<OtherPurchaseRecord> data = getSqlSession().selectList(sqlId("pagingFarm"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 政府端的分页查询
     * @param offset 页码
     * @param limit 每页多少条
     * @param criteria 参数
     * @return Paging<OtherPurchaseRecord>
     */
    public Paging<OtherPurchaseRecord> pagingOtherPurchaseRecordsGov(Integer offset, Integer limit, Map<String, Object> criteria){
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(sqlId("countGov"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<OtherPurchaseRecord> data = getSqlSession().selectList(sqlId("pagingGov"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 查询当前单据的状态
     * @param id 主键id
     * @return Integer
     */
    public Integer findStatusById(Long id){
        return getSqlSession().selectOne(sqlId("findStatusById"),id);
    }

    /**
     * 查询当前的最大单据号
     * @param number number
     * @return String
     */
    public String findCurrentMaxNumber(String number){
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"),number);
    }

    /**
     * 其他采购的统计
     * @param criteria 参数
     * @return Map<String , Object>
     */
    public Map<String , Object> countAmountOfOther(Map<String, Object> criteria){
        return getSqlSession().selectOne(sqlId("countAmountOfOther"),criteria);
    }

    public Double sumTotalPriceForWeb(Map<String, Object> map) {

        return getSqlSession().selectOne(sqlId("sumTotalPriceForWeb"),map);
    }

    public List<Map<String , Object>> findOtherPurchaseInfoWeb(Map<String, Object> map) {

        return getSqlSession().selectList(sqlId("findOtherPurchaseInfoWeb"),map);
    }
}
